<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Nginx学习笔记 | 十三不知</title><meta name="keywords" content="Nginx,基础架构"><meta name="author" content="张十三"><meta name="copyright" content="张十三"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Nginx学习笔记 [TOC] 基础篇 介绍 Nginx是由战斗民族的一位名叫 Igor Sysoev 的程序猿开发的，开源、高性能的 HTTP 服务器和反向代理服务器，也可以作为一个 IMAP&#x2F;POP3 代理服务器。也就是说，Nginx不仅可以托管网站，进行HTTP服务处理，还可以作为反向代理服务器 nginx就是类似apache的提供静态网页的web服务器，相比于apache的多进程多线程的并">
<meta property="og:type" content="article">
<meta property="og:title" content="Nginx学习笔记">
<meta property="og:url" content="http://zuori.gitee.io/zaker/2020/08/25/Nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="十三不知">
<meta property="og:description" content="Nginx学习笔记 [TOC] 基础篇 介绍 Nginx是由战斗民族的一位名叫 Igor Sysoev 的程序猿开发的，开源、高性能的 HTTP 服务器和反向代理服务器，也可以作为一个 IMAP&#x2F;POP3 代理服务器。也就是说，Nginx不仅可以托管网站，进行HTTP服务处理，还可以作为反向代理服务器 nginx就是类似apache的提供静态网页的web服务器，相比于apache的多进程多线程的并">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg">
<meta property="article:published_time" content="2020-08-25T11:46:36.000Z">
<meta property="article:modified_time" content="2021-04-09T09:56:11.697Z">
<meta property="article:author" content="张十三">
<meta property="article:tag" content="Nginx">
<meta property="article:tag" content="基础架构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg"><link rel="shortcut icon" href="https://gitee.com/zuori/kaiscript/raw/master/images/20210325203813.png"><link rel="canonical" href="http://zuori.gitee.io/zaker/2020/08/25/Nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/zaker/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/zaker/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 张十三","link":"链接: ","source":"来源: 十三不知","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-04-09 17:56:11'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://gitee.com/zuori/kaiscript/raw/master/images/20210325192506.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/zaker/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/zaker/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/zaker/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/zaker/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/zaker/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/zaker/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/zaker/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/zaker/">十三不知</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/zaker/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/zaker/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/zaker/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/zaker/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Nginx学习笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-08-25T11:46:36.000Z" title="发表于 2020-08-25 19:46:36">2020-08-25</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-04-09T09:56:11.697Z" title="更新于 2021-04-09 17:56:11">2021-04-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/zaker/categories/Nginx/">Nginx</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Nginx学习笔记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1>Nginx学习笔记</h1>
<p>[TOC]</p>
<h2 id="基础篇">基础篇</h2>
<h3 id="介绍">介绍</h3>
<p>Nginx是由战斗民族的一位名叫 Igor Sysoev 的程序猿开发的，开源、高性能的 HTTP 服务器和反向代理服务器，也可以作为一个 IMAP/POP3 代理服务器。也就是说，Nginx不仅可以托管网站，进行HTTP服务处理，还可以作为反向代理服务器</p>
<p>nginx就是类似apache的提供静态网页的web服务器，相比于apache的多进程多线程的并发模型，而nginx是基于事件的异步IO的并发模型，性能更好，而且nginx是一个轻量级的服务器。</p>
<p>Nginx 出现的初衷是为了解决著名的 C10K 问题而出现的。和传统的 Web Server 不一样，Nginx 使用了异步事件处理机制架构。这种架构可以轻松高效地处理大量的请求，并且非常的节省内存。高性能是Nginx最大的优点。</p>
<p>下面我们简单地比较一下几款流行的 <code>Web</code> 服务器软件的区别。</p>
<ol>
<li>Apache 是一款历史悠久的开源 <code>Web</code> 服务器软件。拥有很多的第三方模块，你想使用的功能基本上都可以找到，避免自己重复造轮子。采用多进程方式处理请求，每个请求都对应于一个进程。在高并发的情况下，多进程处理方式特别的消耗资源，所以天然不适合高并发场景；</li>
<li>Nginx 是年轻(相比<code>Apache</code>)的开源 <code>Web</code> 服务器软件。它也采用 C 语言编写，代码运行效率很高。它采用了<code>epoll</code> (以<code>Linux</code>为例) 事件处理机制作为模型，能够保证非常高的并发量；</li>
<li><code>IIS</code>是微软的<code>Web</code>服务器软件，这个可是收费的哦~~~</li>
<li>GWS-Google(不对外开放)</li>
</ol>
<p>近几年，Nginx的市场占有率越来越高，一度飙升，为什么呢？</p>
<h3 id="为什么选择Nginx">为什么选择Nginx</h3>
<p><strong>1. IO多路复用epoll（IO复用）</strong></p>
<p>如何理解呢？举个例子吧！<br>
有A、B、C三个老师，他们都遇到一个难题，要帮助一个班级的学生解决课堂作业。<br>
老师A采用从第一排开始一个学生一个学生轮流解答的方式去回答问题，老师A浪费了很多时间，并且有的学生作业还没有完成呢，老师就来了，反反复复效率极慢。<br>
老师B是一个忍者，他发现老师A的方法行不通，于是他使用了影分身术，分身出好几个自己同一时间去帮好几个同学回答问题，最后还没回答完，老师B消耗光了能量累倒了。<br>
老师C比较精明，他告诉学生，谁完成了作业举手，有举手的同学他才去指导问题，他让学生主动发声，分开了“并发”。<br>
这个老师C就是Nginx。</p>
<p><strong>2. 轻量级</strong></p>
<ul>
<li>功能模块少 - Nginx仅保留了HTTP需要的模块，其他都用插件的方式，后天添加</li>
<li>代码模块化 - 更适合二次开发，如阿里巴巴Tengine</li>
<li>占用内存小。这得益于 Nginx 使用 C 语言编写，能够高效使用 CPU、内存等系统资源。并且作者自己造了很多的轮子 , 比如 Nginx 自己实现了内存管理系统，动态数组机制等。Nginx 作者对<strong>内存的使用控制简直到了丧心病狂</strong>的地步，所以非常的节省系统资源，特别是内存；</li>
</ul>
<p><strong>3. CPU亲和</strong></p>
<p>把CPU核心和Nginx工作进程绑定，把每个worker进程固定在一个CPU上执行，减少切换CPU的cache miss，从而提高性能。</p>
<p><strong>4. 高并发</strong></p>
<p>高并发。在 Linux 系统上，Nginx 使用了 epoll 机制，能够高效处理大量的连接数。理论上，Nginx 可以同时处理的最大连接数取决于你的机器的物理内存，上不封顶；</p>
<p><strong>5. 高可靠性</strong></p>
<p>Nginx 的高可靠性主要体现在两方面:</p>
<p><strong>(一)</strong>：Nginx 使用了 <strong>Master-Worker 机制</strong>，真正处理请求的是<code>Worker</code>进程。<code>Master</code>进程可以监控<code>Worker</code>进程的运行状况，当某个<code>Worker</code>进程因意外原因退出的时候，<code>Master</code>会重新启动<code>Worker</code>进程；</p>
<p><strong>(二)</strong>：Nginx 的内部框架非常优秀。它的各个模块都非常简单，所以也非常的稳定。</p>
<p><strong>6. 热部署</strong></p>
<p>可能大家觉得这个原因并不重要，其实在实际的线上环境是非常重要的。代码上线之后，我们只需要执行 <code>nginx -s reload</code> 命令就可以完成 Nginx 的重启，其他的交给 Nginx 就可以了。</p>
<h3 id="安装与使用">安装与使用</h3>
<h4 id="前置准备">前置准备</h4>
<p>机器：我这里使用的是Centos 7.6版本linux</p>
<p>安装所需插件：</p>
<ol>
<li>
<p>首先必须要有gcc的，是用来编译与安装源码包的</p>
<p>一般服务器上都有</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc -v   </span><br></pre></td></tr></table></figure>
<p>如果找不到可以自行下载</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yum -y install gcc automake autoconf libtool make</span><br><span class="line">yum install -y gcc-c++</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>pcre、pcre-devel安装</p>
<p>PCRE的全称是Perl Compatible Regular Expressions，是一个兼容perl的正则表达式库，使用c语言实现，性能非常的高。nginx使用PCRE实现了http rewrite功能，所以nginx的http模块是使用pcre来解析正则表达式，所以需要安装pcre库。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y pcre pcre-devel</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>zlib安装</p>
<p>zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip，所以需要安装</p>
<p>安装命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y zlib zlib-devel</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>安装openssl</p>
<p>openssl是web安全通信的基石，没有openssl，可以说我们的信息都是在裸奔，该应用程序可以使用这个包来进行安全通信，避免窃听，同时确认另一端连接者的身份</p>
<p>安装命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y openssl openssl-devel</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="获取Nginx源码安装包"><strong>获取Nginx源码安装包</strong></h4>
<p>打开<a target="_blank" rel="noopener" href="http://nginx.org/">nginx官网</a>，点击右侧列表的 download 进入源码下载页面。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724215811.png" alt=""></p>
<p>我们可以看到 Nginx 提供了三种版本的下载，分别是 <strong>开发版本、稳定版本、过期版本</strong>。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724220228.png" alt=""></p>
<p>一般都是选择稳定版本进行下载，这里我下载的就是<strong>nginx-1.18.0</strong></p>
<p>nginx-1.18.0 是 linux 版本的源码，nginx/Windows-1.18.0 是 windows 版本的源码。两个 pgp 分别是对应平台版本源码经过 PGP 加密之后的签名，我们可以通过这个签名验证下载的内容是否正确。</p>
<p>然后就可以点击右键复制下载链接，在服务器中用wget进行下载</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget http://nginx.org/download/nginx-1.18.0.tar.gz</span><br></pre></td></tr></table></figure>
<p>可以看到正在下载<img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724221059.png" alt=""></p>
<h4 id="解压源码"><strong>解压源码</strong></h4>
<p>等待下载完成</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf nginx-1.18.0.tar.gz</span><br></pre></td></tr></table></figure>
<p>解压完成后就可以看到一个nginx-1.18.0的文件夹</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724222308.png" alt=""></p>
<h4 id="编译源码"><strong>编译源码</strong></h4>
<p>下面进入到刚才解压的 nginx 源码目录中开始对源码进行编译。</p>
<p>在文章的开头我们说过，一般情况下，Linux 的软件在编译的时候要经过三个步骤，configure/make以及make install，所以nginx也是一样的。</p>
<p>所以，切换到nginx的根目录里，可以看到configure文件</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724224332.png" alt=""></p>
<h5 id="configure配置文件大致解读">configure配置文件大致解读</h5>
<p>（如果不想了解的也没关系，可以直接跳过）</p>
<p>在这里，configure脚本的作用就是生成Makefile文件。这个脚本可以带一些参数，用于控制程序的编译行为，参数大致分为五类：通用配置类，编译器配置类，邮件配置类，路径配置类，第三方模块配置类。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724225246.png" alt=""></p>
<p>可以使用<code>./configure --help</code>去查看这个脚本包含的所有参数，这里面就包含了上面的五类配置选项</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br></pre></td><td class="code"><pre><span class="line">--help                             print this message</span><br><span class="line"></span><br><span class="line">--prefix&#x3D;PATH                      set installation prefix</span><br><span class="line">--sbin-path&#x3D;PATH                   set nginx binary pathname</span><br><span class="line">--modules-path&#x3D;PATH                set modules path</span><br><span class="line">--conf-path&#x3D;PATH                   set nginx.conf pathname</span><br><span class="line">--error-log-path&#x3D;PATH              set error log pathname</span><br><span class="line">--pid-path&#x3D;PATH                    set nginx.pid pathname</span><br><span class="line">--lock-path&#x3D;PATH                   set nginx.lock pathname</span><br><span class="line"></span><br><span class="line">--user&#x3D;USER                        set non-privileged user for</span><br><span class="line">                                   worker processes</span><br><span class="line">--group&#x3D;GROUP                      set non-privileged group for</span><br><span class="line">                                   worker processes</span><br><span class="line"></span><br><span class="line">--build&#x3D;NAME                       set build name</span><br><span class="line">--builddir&#x3D;DIR                     set build directory</span><br><span class="line"></span><br><span class="line">--with-select_module               enable select module</span><br><span class="line">--without-select_module            disable select module</span><br><span class="line">--with-poll_module                 enable poll module</span><br><span class="line">--without-poll_module              disable poll module</span><br><span class="line"></span><br><span class="line">--with-threads                     enable thread pool support</span><br><span class="line"></span><br><span class="line">--with-file-aio                    enable file AIO support</span><br><span class="line"></span><br><span class="line">--with-http_ssl_module             enable ngx_http_ssl_module</span><br><span class="line">--with-http_v2_module              enable ngx_http_v2_module</span><br><span class="line">--with-http_realip_module          enable ngx_http_realip_module</span><br><span class="line">--with-http_addition_module        enable ngx_http_addition_module</span><br><span class="line">--with-http_xslt_module            enable ngx_http_xslt_module</span><br><span class="line">--with-http_xslt_module&#x3D;dynamic    enable dynamic ngx_http_xslt_module</span><br><span class="line">--with-http_image_filter_module    enable ngx_http_image_filter_module</span><br><span class="line">--with-http_image_filter_module&#x3D;dynamic</span><br><span class="line">                                   enable dynamic ngx_http_image_filter_module</span><br><span class="line">--with-http_geoip_module           enable ngx_http_geoip_module</span><br><span class="line">--with-http_geoip_module&#x3D;dynamic   enable dynamic ngx_http_geoip_module</span><br><span class="line">--with-http_sub_module             enable ngx_http_sub_module</span><br><span class="line">--with-http_dav_module             enable ngx_http_dav_module</span><br><span class="line">--with-http_flv_module             enable ngx_http_flv_module</span><br><span class="line">--with-http_mp4_module             enable ngx_http_mp4_module</span><br><span class="line">--with-http_gunzip_module          enable ngx_http_gunzip_module</span><br><span class="line">--with-http_gzip_static_module     enable ngx_http_gzip_static_module</span><br><span class="line">--with-http_auth_request_module    enable ngx_http_auth_request_module</span><br><span class="line">--with-http_random_index_module    enable ngx_http_random_index_module</span><br><span class="line">--with-http_secure_link_module     enable ngx_http_secure_link_module</span><br><span class="line">--with-http_degradation_module     enable ngx_http_degradation_module</span><br><span class="line">--with-http_slice_module           enable ngx_http_slice_module</span><br><span class="line">--with-http_stub_status_module     enable ngx_http_stub_status_module</span><br><span class="line"></span><br><span class="line">--without-http_charset_module      disable ngx_http_charset_module</span><br><span class="line">--without-http_gzip_module         disable ngx_http_gzip_module</span><br><span class="line">--without-http_ssi_module          disable ngx_http_ssi_module</span><br><span class="line">--without-http_userid_module       disable ngx_http_userid_module</span><br><span class="line">--without-http_access_module       disable ngx_http_access_module</span><br><span class="line">--without-http_auth_basic_module   disable ngx_http_auth_basic_module</span><br><span class="line">--without-http_mirror_module       disable ngx_http_mirror_module</span><br><span class="line">--without-http_autoindex_module    disable ngx_http_autoindex_module</span><br><span class="line">--without-http_geo_module          disable ngx_http_geo_module</span><br><span class="line">--without-http_map_module          disable ngx_http_map_module</span><br><span class="line">--without-http_split_clients_module disable ngx_http_split_clients_module</span><br><span class="line">--without-http_referer_module      disable ngx_http_referer_module</span><br><span class="line">--without-http_rewrite_module      disable ngx_http_rewrite_module</span><br><span class="line">--without-http_proxy_module        disable ngx_http_proxy_module</span><br><span class="line">--without-http_fastcgi_module      disable ngx_http_fastcgi_module</span><br><span class="line">--without-http_uwsgi_module        disable ngx_http_uwsgi_module</span><br><span class="line">--without-http_scgi_module         disable ngx_http_scgi_module</span><br><span class="line">--without-http_grpc_module         disable ngx_http_grpc_module</span><br><span class="line">--without-http_memcached_module    disable ngx_http_memcached_module</span><br><span class="line">--without-http_limit_conn_module   disable ngx_http_limit_conn_module</span><br><span class="line">--without-http_limit_req_module    disable ngx_http_limit_req_module</span><br><span class="line">--without-http_empty_gif_module    disable ngx_http_empty_gif_module</span><br><span class="line">--without-http_browser_module      disable ngx_http_browser_module</span><br><span class="line">--without-http_upstream_hash_module</span><br><span class="line">                                   disable ngx_http_upstream_hash_module</span><br><span class="line">--without-http_upstream_ip_hash_module</span><br><span class="line">                                   disable ngx_http_upstream_ip_hash_module</span><br><span class="line">--without-http_upstream_least_conn_module</span><br><span class="line">                                   disable ngx_http_upstream_least_conn_module</span><br><span class="line">--without-http_upstream_random_module</span><br><span class="line">                                   disable ngx_http_upstream_random_module</span><br><span class="line">--without-http_upstream_keepalive_module</span><br><span class="line">                                   disable ngx_http_upstream_keepalive_module</span><br><span class="line">--without-http_upstream_zone_module</span><br><span class="line">                                   disable ngx_http_upstream_zone_module</span><br><span class="line"></span><br><span class="line">--with-http_perl_module            enable ngx_http_perl_module</span><br><span class="line">--with-http_perl_module&#x3D;dynamic    enable dynamic ngx_http_perl_module</span><br><span class="line">--with-perl_modules_path&#x3D;PATH      set Perl modules path</span><br><span class="line">--with-perl&#x3D;PATH                   set perl binary pathname</span><br><span class="line"></span><br><span class="line">--http-log-path&#x3D;PATH               set http access log pathname</span><br><span class="line">--http-client-body-temp-path&#x3D;PATH  set path to store</span><br><span class="line">                                   http client request body temporary files</span><br><span class="line">--http-proxy-temp-path&#x3D;PATH        set path to store</span><br><span class="line">                                   http proxy temporary files</span><br><span class="line">--http-fastcgi-temp-path&#x3D;PATH      set path to store</span><br><span class="line">                                   http fastcgi temporary files</span><br><span class="line">--http-uwsgi-temp-path&#x3D;PATH        set path to store</span><br><span class="line">                                   http uwsgi temporary files</span><br><span class="line">--http-scgi-temp-path&#x3D;PATH         set path to store</span><br><span class="line">                                   http scgi temporary files</span><br><span class="line"></span><br><span class="line">--without-http                     disable HTTP server</span><br><span class="line">--without-http-cache               disable HTTP cache</span><br><span class="line"></span><br><span class="line">--with-mail                        enable POP3&#x2F;IMAP4&#x2F;SMTP proxy module</span><br><span class="line">--with-mail&#x3D;dynamic                enable dynamic POP3&#x2F;IMAP4&#x2F;SMTP proxy module</span><br><span class="line">--with-mail_ssl_module             enable ngx_mail_ssl_module</span><br><span class="line">--without-mail_pop3_module         disable ngx_mail_pop3_module</span><br><span class="line">--without-mail_imap_module         disable ngx_mail_imap_module</span><br><span class="line">--without-mail_smtp_module         disable ngx_mail_smtp_module</span><br><span class="line"></span><br><span class="line">--with-stream                      enable TCP&#x2F;UDP proxy module</span><br><span class="line">--with-stream&#x3D;dynamic              enable dynamic TCP&#x2F;UDP proxy module</span><br><span class="line">--with-stream_ssl_module           enable ngx_stream_ssl_module</span><br><span class="line">--with-stream_realip_module        enable ngx_stream_realip_module</span><br><span class="line">--with-stream_geoip_module         enable ngx_stream_geoip_module</span><br><span class="line">--with-stream_geoip_module&#x3D;dynamic enable dynamic ngx_stream_geoip_module</span><br><span class="line">--with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module</span><br><span class="line">--without-stream_limit_conn_module disable ngx_stream_limit_conn_module</span><br><span class="line">--without-stream_access_module     disable ngx_stream_access_module</span><br><span class="line">--without-stream_geo_module        disable ngx_stream_geo_module</span><br><span class="line">--without-stream_map_module        disable ngx_stream_map_module</span><br><span class="line">--without-stream_split_clients_module</span><br><span class="line">                                   disable ngx_stream_split_clients_module</span><br><span class="line">--without-stream_return_module     disable ngx_stream_return_module</span><br><span class="line">--without-stream_upstream_hash_module</span><br><span class="line">                                   disable ngx_stream_upstream_hash_module</span><br><span class="line">--without-stream_upstream_least_conn_module</span><br><span class="line">                                   disable ngx_stream_upstream_least_conn_module</span><br><span class="line">--without-stream_upstream_random_module</span><br><span class="line">                                   disable ngx_stream_upstream_random_module</span><br><span class="line">--without-stream_upstream_zone_module</span><br><span class="line">                                   disable ngx_stream_upstream_zone_module</span><br><span class="line"></span><br><span class="line">--with-google_perftools_module     enable ngx_google_perftools_module</span><br><span class="line">--with-cpp_test_module             enable ngx_cpp_test_module</span><br><span class="line"></span><br><span class="line">--add-module&#x3D;PATH                  enable external module</span><br><span class="line">--add-dynamic-module&#x3D;PATH          enable dynamic external module</span><br><span class="line"></span><br><span class="line">--with-compat                      dynamic modules compatibility</span><br><span class="line"></span><br><span class="line">--with-cc&#x3D;PATH                     set C compiler pathname</span><br><span class="line">--with-cpp&#x3D;PATH                    set C preprocessor pathname</span><br><span class="line">--with-cc-opt&#x3D;OPTIONS              set additional C compiler options</span><br><span class="line">--with-ld-opt&#x3D;OPTIONS              set additional linker options</span><br><span class="line">--with-cpu-opt&#x3D;CPU                 build for the specified CPU, valid values:</span><br><span class="line">                                   pentium, pentiumpro, pentium3, pentium4,</span><br><span class="line">                                   athlon, opteron, sparc32, sparc64, ppc64</span><br><span class="line"></span><br><span class="line">--without-pcre                     disable PCRE library usage</span><br><span class="line">--with-pcre                        force PCRE library usage</span><br><span class="line">--with-pcre&#x3D;DIR                    set path to PCRE library sources</span><br><span class="line">--with-pcre-opt&#x3D;OPTIONS            set additional build options for PCRE</span><br><span class="line">--with-pcre-jit                    build PCRE with JIT compilation support</span><br><span class="line"></span><br><span class="line">--with-zlib&#x3D;DIR                    set path to zlib library sources</span><br><span class="line">--with-zlib-opt&#x3D;OPTIONS            set additional build options for zlib</span><br><span class="line">--with-zlib-asm&#x3D;CPU                use zlib assembler sources optimized</span><br><span class="line">                                   for the specified CPU, valid values:</span><br><span class="line">                                   pentium, pentiumpro</span><br><span class="line"></span><br><span class="line">--with-libatomic                   force libatomic_ops library usage</span><br><span class="line">--with-libatomic&#x3D;DIR               set path to libatomic_ops library sources</span><br><span class="line"></span><br><span class="line">--with-openssl&#x3D;DIR                 set path to OpenSSL library sources</span><br><span class="line">--with-openssl-opt&#x3D;OPTIONS         set additional build options for OpenSSL</span><br><span class="line"></span><br><span class="line">--with-debug                       enable debug logging</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>这里确实是有点多，每个参数大概的意思也可以自行翻译</p>
<p>这里列举下常用的参数</p>
<ol>
<li>
<p>常用的通用配置类</p>
<table>
<thead>
<tr>
<th style="text-align:left">选项</th>
<th style="text-align:left">功能</th>
<th style="text-align:left">备注</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>--prefix=&lt;PATH&gt;</code></td>
<td style="text-align:left"><code>nginx</code>的前缀路径</td>
<td style="text-align:left">其他路径都依赖于该路径，默认为<code>/usr/local/nginx</code></td>
</tr>
<tr>
<td style="text-align:left"><code>--sbin-path=&lt;PATH&gt;</code></td>
<td style="text-align:left">生成的可执行程序保存路径</td>
<td style="text-align:left">默认为 <code>prefix</code> + <code>/sbin/nginx</code></td>
</tr>
<tr>
<td style="text-align:left"><code>--conf-path=&lt;PATH&gt;</code></td>
<td style="text-align:left">配置文件路径</td>
<td style="text-align:left">默认为 <code>prefix</code> + <code>/conf/nginx.conf</code></td>
</tr>
<tr>
<td style="text-align:left"><code>--error-log-path=&lt;PATH&gt;</code></td>
<td style="text-align:left">错误日志路径</td>
<td style="text-align:left">默认为 <code>prefix</code> + <code>/logs/error.log</code></td>
</tr>
<tr>
<td style="text-align:left"><code>--pid-path=&lt;PATH&gt;=&lt;PATH&gt;</code></td>
<td style="text-align:left"><code>pid</code>文件的保存路径，</td>
<td style="text-align:left">默认为 <code>prefix</code> + <code>/logs/nginx.pid</code></td>
</tr>
<tr>
<td style="text-align:left"><code>--lock-path=PATH</code></td>
<td style="text-align:left">lock文件的放置路径</td>
<td style="text-align:left">默认为<code>perfix</code>+<code>/logs/nginx.lock</code></td>
</tr>
</tbody>
</table>
</li>
<li>
<p>编译器的相关参数</p>
<table>
<thead>
<tr>
<th>编译参数</th>
<th>意义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-with-cc=PATH</code></td>
<td>编译器的路径</td>
</tr>
<tr>
<td><code>--with-cpp=PATH</code></td>
<td>C预编译器的路径</td>
</tr>
<tr>
<td><code>--with-cc-opt=OPTIONS</code></td>
<td>如果希望在Nginx编译期间指定加入一些编译选项，如指定宏或者使用-I 加入某些需要包含的目录这时可以使用该参数达成目的。<br/> --with-ld-opt=OPTIONS	最终的二进制可执行文件是由编译后生成的目标文件与一些第三方库连接而成的，在执行链接操作时可能会需要指定的链接参数，–with-ld-opt就是用于加入链接时的参数。例如，如果我们希望将某个库链接到Nginx程序中，需要在这里加入–with-ld-opt=-libraryName - L libraryPath，其中libraryName是目标库的名称，libraryPath则是目标库所在的路径。</td>
</tr>
<tr>
<td><code>--with-cpu-opt=CPU</code></td>
<td>指定CPU的处理器架构，只能从以下值中选择：pentium pentiumpro pentium3 pentium4 athlon opteron sparc32 sparc64 ppc64</td>
</tr>
</tbody>
</table>
</li>
<li>
<p>第三方依赖</p>
<p>PCRE的设置参数</p>
<table>
<thead>
<tr>
<th>PCRE库的设置参数</th>
<th>意义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>--without-pcre</code></td>
<td>如果确认Nginx不用解析正则表达式，也是就是说，nginx.conf配置文件中不会出现正则表达式，那就可以使用这个参数。</td>
</tr>
<tr>
<td><code>--with-pcre</code></td>
<td>强制使用PCRE库</td>
</tr>
<tr>
<td><code>--with-pcre=DIR</code></td>
<td>指定PCRE库的源码位置</td>
</tr>
<tr>
<td><code>--with-pcre-opt=OPTIONS</code></td>
<td>编译PCRE源码时希望加入的编译选项</td>
</tr>
</tbody>
</table>
<p>​	OpenSSL 的设置参数</p>
</li>
</ol>
<table>
<thead>
<tr>
<th><strong>OpenSSL库的设置参数</strong></th>
<th>意义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>--with-openssl=DIR</code></td>
<td>指定openssl库的源码位置，在编译时会进入该目录编译openssl源码。</td>
</tr>
<tr>
<td><code>---with-openssl-opt=OPTIONS</code></td>
<td>编译openssl源码时希望加入的编译选项</td>
</tr>
</tbody>
</table>
<p>第三方依赖还有很多，总共分为两种，一种是默认自动编译到<code>nginx</code>可执行文件中的模块，一种是没有自动编译到<code>nginx</code>可执行文件中的。</p>
<p>对于前者，我们可以使用<code>--without-XXX_module</code>的方式来取消自动编译。比如<code>--without-pcre</code>就是不使用正则表达式pcre库。</p>
<p>对于后者，我们可以使用<code>--with--XX_module</code>的方式将模块编译到可执行程序中。比如我们可以通过<code>--with-http_geoip_module</code>命令将地理位置的<code>geoip</code>模块编译到最终的<code>nginx</code>可执行程序中。</p>
<p>具体的编译安装的方法可以参考官方的这篇文章<a target="_blank" rel="noopener" href="http://nginx.org/en/docs/configure.html">configure</a>。</p>
<h5 id="编译安装">编译安装</h5>
<p>这里configure文件的参数大致就学到这里，下面开始安装nginx</p>
<ol>
<li>执行configure脚本</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./configure --prefix=/usr/local/nginx	</span><br></pre></td></tr></table></figure>
<p>​	我们可以在屏幕上看到输出一大堆的信息，这些带<code>checking</code>字样的是<code>configure</code>脚本自动判断当前操作系统的一些特性，比如获取当前操作系统的内核版本号，是否支持<code>epoll</code>，判断<code>int</code>类型的长度等等。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724233419.png" alt=""></p>
<ol start="2">
<li>
<p>执行make和make install</p>
<p>经过上一步的<code>configure</code>，已经生成了<code>Makefile</code>文件，我们就可以通过执行<code>make</code>命令对<code>nginx</code>进行编译</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make &amp;&amp; make install</span><br></pre></td></tr></table></figure>
<p>执行成功就安装完成了，这样一个Nginx服务器就安装完成。</p>
<p>可以做一下测试，刚才执行configure脚本的时候参数是–prefix=/usr/local/nginx，所以安装好的nginx路径应该就在/usr/local/nginx下。</p>
<p>设置开机启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo &#39;&#x2F;usr&#x2F;src&#x2F;nginx&#x2F;sbin&#x2F;nginx&#39; &gt;&gt; &#x2F;etc&#x2F;rc.local</span><br></pre></td></tr></table></figure>
<p>直接用tree查看目录结构</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200724235440.png" alt=""></p>
</li>
</ol>
<p>其中/conf/nginx.conf文件是主要的配置文件，html存放的是静态网页文件，logs里面存放的是主要的nginx日志，sbin下是nginx的可执行文件。</p>
<h5 id="启动nginx">启动nginx</h5>
<p>启动nginx很简单，直接进入sbin目录下，执行nginx文件就可以启动，还有就是nginx启动的时候可以带很多参数：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>？，h</td>
<td>输出help信息</td>
</tr>
<tr>
<td>v</td>
<td>输出版本信息</td>
</tr>
<tr>
<td>V</td>
<td>输出执行<code>./configure</code>时候的参数</td>
</tr>
<tr>
<td>t</td>
<td>测试配置文件是否正确</td>
</tr>
<tr>
<td>T</td>
<td>输出配置文件内容</td>
</tr>
<tr>
<td>p</td>
<td>设置prefix</td>
</tr>
<tr>
<td>c</td>
<td>执行配置文件路径</td>
</tr>
<tr>
<td>g</td>
<td>在命令行中指定全局配置参数</td>
</tr>
<tr>
<td>s</td>
<td>向nginx发送信号</td>
</tr>
</tbody>
</table>
<p>现在直接去sbin目录下执行nginx的可执行文件，启动nginx服务器，这种情况下使用的是默认的配置文件，即<code>/usr/local/nginx/conf/nginx.conf</code></p>
<p>查看nginx是启动成功</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep nginx</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725001605.png" alt=""></p>
<p>如果服务器80端口对外开放的，就可以直接去浏览器输入服务器IP进行测试，可以看到nginx的访问界面，nginx启动成功。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725001230.png" alt=""></p>
<h5 id="停止nginx">停止nginx</h5>
<p>nginx提供了两种方法来停止服务：优雅关闭和快速关闭。</p>
<p>快速关闭:  nginx强制停止服务，master和worker进程收到信号之后，会立即结束运行。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-s stop</span><br></pre></td></tr></table></figure>
<p>优雅关闭: nginx会按照下面的步骤停止服务：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-s quite</span><br></pre></td></tr></table></figure>
<p>1). 关闭监听端口，停止接收新的连接；<br>
2). Nginx处理完当前的所有。剩余请求；<br>
3). 停止Nginx服务</p>
<p>其实这两种方式都是对kill命令的一个封装，我们也可以直接使用kill命令完成相同的功能。只不过kill命令要知道nginx的master进程的pid，我们可以结合ps命令来查找进程号。<br>
其实还有一种方法来获取master进程的pid，那就是nginx.pid文件，这个文件的内容就是master进程的pid，大家知道这种方法就行了，尽量使用nginx提供的-s命令。</p>
<h5 id="重新加载配置文件">重新加载配置文件</h5>
<p>nginx是支持热加载的,当配置文件发生改变之后，nginx可以不用断开服务，直接重新加载配置就行了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx -s reload</span><br></pre></td></tr></table></figure>
<h3 id="基本配置文件语法">基本配置文件语法</h3>
<p>nginx是模块化的系统，整个系统是分成一个个模块的。每个模块负责不同的功能。比如http_gzip_static_module就是负责压缩的，http_ssl_module就是负责加密的，如果不用某个模块的话，也可以去掉，可以让整个nginx变得小巧，更适合自己。在上面的configure指令中带了很多参数，就是在这里编译之前可以加入某些模块或去掉某些模块的。</p>
<p>要用的模块已经被编译进nginx了，成为nginx的一部分了，那要怎么用这些模块呢？那就得通过配置文件，这跟传统的linux服务差不多，都是通过配置文件来改变功能。nginx的模块是通过一个叫指令(directive)的东西来用的。整个配置文件都是由指令来控制的。nginx也有自己内置的指令，比如events, http, server, 和 location等，下面会提到的。</p>
<h4 id="配置文件位置">配置文件位置</h4>
<p>由于我们在编译的时候设置了<code>--prefix</code>选项，指定前缀路径是<code>/usr/local/nginx</code>，那么默认的配置文件就在<code>/usr/local/nginx/conf/nginx.conf</code>。<br>
如果找不到，使用上面安装解读提到的-t参数，在测试配置文件语法是否正确的时候就会打印出配置文件的位置。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725003611.png" alt=""></p>
<h4 id="配置文件结构">配置文件结构</h4>
<p>接下来再开始查看下默认的配置文件(删除掉注释)：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#user  nobody;</span></span><br><span class="line"><span class="attribute">worker_processes</span>  <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="section">events</span> &#123;</span><br><span class="line">    <span class="attribute">worker_connections</span>  <span class="number">1024</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">    <span class="attribute">include</span>       mime.types;    <span class="comment">#引入文件</span></span><br><span class="line">    <span class="attribute">default_type</span>  application/octet-stream;</span><br><span class="line">    <span class="attribute">sendfile</span>        <span class="literal">on</span>;</span><br><span class="line">    <span class="attribute">keepalive_timeout</span>  <span class="number">65</span>;</span><br><span class="line">    <span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>       <span class="number">80</span>;</span><br><span class="line">        <span class="attribute">server_name</span>  localhost;</span><br><span class="line"></span><br><span class="line">        <span class="comment">#charset koi8-r;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">#access_log  logs/host.access.log  main;</span></span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> / &#123;</span><br><span class="line">            <span class="attribute">root</span>   html;</span><br><span class="line">            <span class="attribute">index</span>  index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="attribute">error_page</span>   <span class="number">500</span> <span class="number">502</span> <span class="number">503</span> <span class="number">504</span>  /50x.html;</span><br><span class="line">        <span class="attribute">location</span> = /50x.html &#123;</span><br><span class="line">            <span class="attribute">root</span>   html;</span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>去掉注释后,在这个文件中，先不管上面三行，就是由两个block(块)组成的。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">events &#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>块和块之间还可以嵌套的。例如http下面可以放server。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">http &#123;</span><br><span class="line"> server &#123;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这就是一个最小的配置文件，实现了一个基本的http服务器。</p>
<p>nginx采用了简单的文本格式的配置文件，下图总结了nginx指令一些特性。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725005405.png" alt=""></p>
<h5 id="指令的特点">指令的特点</h5>
<p>按照指令的特点，可以将指令分为四种形式，分别是注释，简单指令，块指令和包含指令。</p>
<p>注释：<code>nginx</code>把以<code>#</code>开头的<code>单行</code>内容视为注释，在解析配置文件时候会忽略这部分内容。注意，注释必须是单行的，如果多行注释，那么每行都要以<code>#</code>开头。</p>
<p><strong>注释</strong>：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#user  nobody;    #设置nginx服务的系统使用用户</span></span><br></pre></td></tr></table></figure>
<p><strong>简单指令:</strong> 以分号结尾，比如下面的：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">worker_processes</span>  <span class="number">1</span>;   <span class="comment">#工作进程数，一般与CPU核数一致</span></span><br><span class="line"><span class="attribute">error_log</span>  <span class="comment">#nginx的错误日志</span></span><br><span class="line">pid   <span class="comment">#nginx启动时的pid</span></span><br></pre></td></tr></table></figure>
<p><strong>块指令</strong>: 将一组组合组合在一起，使用花括号<code>&#123;&#125;</code>进行分组，如：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">events</span> &#123;</span><br><span class="line">    <span class="attribute">worker_connections</span>  <span class="number">1024</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里的<code>events</code>就是一个块指令，上面的<code>http</code>也是一个块指令。</p>
<p><strong>包含指令</strong>：将一些指令放置到一个单独的文件中，然后使用<code>include</code>指令将这些文件包含到配置文件中，就好像在把这个文件的内容填充到了这个位置一样,就和java里面的import意思一样，就是引用包含。为什么要用包含指令呢？</p>
<p>nginx.conf这个是主配置文件。有时候仅仅一个配置文件是不够的，由其是当配置文件很大时，总不能全部逻辑塞一个文件里，所以配置文件也是需要来管理的。</p>
<p>比如指令：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#引入mime.types文件</span></span><br><span class="line"><span class="attribute">include</span>       mime.types;</span><br><span class="line"><span class="comment">#会自动包含/usr/local/nginx/user_conf/下的所有的后缀名为.conf的文件</span></span><br><span class="line"><span class="attribute">include</span> /usr/local/nginx/user_conf/<span class="regexp">*.conf</span>;</span><br><span class="line"><span class="comment">#会自动包含/usr/local/nginx/user_conf/下的所有文件</span></span><br><span class="line"><span class="attribute">include</span> /usr/local/nginx/user_conf/*;</span><br></pre></td></tr></table></figure>
<h5 id="指令的层次">指令的层次</h5>
<p>如果按照层次划分，<code>nginx</code>的指令分为了多个层级，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">user  nobody;  # main 级别配置，nginx核心功能</span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">    use epoll; # events级别配置, nginx事件机制</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http &#123; # http 级别  nginx的http核心模块</span><br><span class="line"></span><br><span class="line">    server &#123; # server 级别，一个server代表一个虚拟主机</span><br><span class="line">        </span><br><span class="line">        location &#123; </span><br><span class="line">            # location级别, 一个location代表一类url</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>一个<code>http</code>模块可以包含多个<code>server</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">http &#123;</span><br><span class="line">  ... ...    #后面再详细介绍 http 配置项目</span><br><span class="line"></span><br><span class="line">  server &#123;</span><br><span class="line">    listen 80             #监听端口;</span><br><span class="line">    server_name localhost       #地址</span><br><span class="line"></span><br><span class="line">				location &#x2F; &#123;            #访问首页路径</span><br><span class="line">        root &#x2F;xxx&#x2F;xxx&#x2F;index.html    #默认目录</span><br><span class="line">        index index.html index.htm   #默认文件</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">      error_page 500 504  &#x2F;50x.html  #当出现以上状态码时从新定义到50x.html</span><br><span class="line">      location &#x3D; &#x2F;50x.html &#123;       #当访问50x.html时</span><br><span class="line">      root &#x2F;xxx&#x2F;xxx&#x2F;html       #50x.html 页面所在位置</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  server &#123;</span><br><span class="line">    ... ...</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>同样的，一个<code>server</code>可以包含多个<code>location</code>。</p>
<p>有些指令可以同时存在多个不同的<code>级别</code>中，那么这里就存在了一个指令覆盖问题。</p>
<p>如果我们存在下面的配置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">http &#123;</span><br><span class="line">    server &#123; # server1</span><br><span class="line">        server_name my.first.server.com</span><br><span class="line">        root &#x2F;root&#x2F;server1; </span><br><span class="line">        </span><br><span class="line">        location &#x2F; &#123; #locationA</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        location &#x2F;img &#123; #locationB</span><br><span class="line">            root &#x2F;root&#x2F;img;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>那么在<code>locationB</code>中的<code>root</code>会覆盖<code>server1</code>中的<code>root</code>，而<code>locationA</code>中由于没有<code>root</code>，所以会继承它的上一层级的<code>root</code>。</p>
<p>所以在<code>nginx</code>中，下一层级如果没有配置指令的话，会继承上一层级的同一个配置的值。</p>
<p>指令和指令之间是有层级和继承关系的。比如http内的指令会影响到server的。</p>
<p>http那部分除非必要，我们不动它，假如你现在要部署一个web服务，那就在/etc/nginx/conf.d/目录下新增一个文件就好了。</p>
<p>http和events还有mail是同级的。http就是跟web有关的。</p>
<p>基本的指令语法大概就是这么些，下来学习一点实际的应用</p>
<h2 id="场景实现">场景实现</h2>
<h3 id="静态资源WEB服务">静态资源WEB服务</h3>
<p>Nginx可以做静态资源web服务器，说白了就是可以<strong>静态资源类型</strong>的文件直接代理出来。</p>
<p>这里对静态资源类型做一下解释：</p>
<p>非服务器动态运行生成的文件，换句话说，就是可以直接在服务器上找到对应文件的请求</p>
<ol>
<li>浏览器端渲染：HTML,CSS,JS</li>
<li>图片：JPEG,GIF,PNG</li>
<li>视频：FLV,MPEG</li>
<li>文件：TXT，任意下载文件</li>
</ol>
<p>下面练习一个案例来使用nginx做一个静态资源服务器，将服务器上图片直接对外可以访问</p>
<p>先写配置文件：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">user</span>  nobody;     </span><br><span class="line"><span class="attribute">worker_processes</span>  <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">#error_log  logs/error.log;</span></span><br><span class="line"></span><br><span class="line">http&#123;</span><br><span class="line">   <span class="section">server</span>  &#123;</span><br><span class="line">           <span class="attribute">listen</span>      <span class="number">1880</span>;</span><br><span class="line">        <span class="attribute">server_name</span>  localhost;</span><br><span class="line">        <span class="comment">#charset koi8-r; </span></span><br><span class="line">        <span class="comment">#access_log  logs/host.access.log  main;</span></span><br><span class="line">         <span class="attribute">location</span> / &#123;</span><br><span class="line">             <span class="attribute">root</span>   /home/zaker/image;</span><br><span class="line">             <span class="attribute">autoindex</span> <span class="literal">on</span>;</span><br><span class="line">             <span class="attribute">index</span>  index.html index.htm;</span><br><span class="line">          &#125;</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>listen  监听1880端口</p>
<p>root  /home/zaker/image;    这句话是映射到图片保存的服务器真实路径</p>
<p>autoindex on;                         这句话是开启浏览目录权限</p>
<p>然后执行以下reload，重新加载下nginx配置文件，成功后，访问：ip:1880</p>
<p>然后发现报错了：</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725132707.png" alt=""></p>
<p>403，一般都是权限的问题，所以去看以下图片文件夹的权限</p>
<p>下来就来查看下权限</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls -l &#x2F;homer&#x2F;zaker&#x2F;image&#x2F;</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725132952.png" alt=""></p>
<p>发现静态文件的所属权限是zaker这个用户的，然后再回头看最上面的全局指令，使用的nobody，nobody是没有这个文件的权限的。</p>
<p>操作系统中的所有资源都具有所属者以及其他各种访问权限，尤其是linux最为明显。这种权限控制可以让操作系统变得很安全，防止<code>黑客</code>破坏我们的系统，<code>Nginx</code>提供了<code>user</code>全局配置指令来设置<code>worker</code>进程的所属者，这样就可以让<code>Nginx</code>访问特定权限的资源，保护我们服务器的稳定性。</p>
<p>所以就两种方案：</p>
<ol>
<li>给使用的用户添加可读权限</li>
<li>修改user的使用用户为zaker</li>
</ol>
<p>这里我采用第二种：</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725133834.png" alt=""></p>
<p>重新reload配置文件，然后再尝试：ip:1800</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725134028.png" alt=""></p>
<p>文件点进去就可以看到图片：</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725135935.png" alt=""></p>
<p>可以很清楚的看到文件现在已经可以被外部所访问了，这样一个最简单的静态资源服务器就这么做好了。</p>
<h3 id="反向代理">反向代理</h3>
<p>要说反向代理，那么肯定也有正向代理，这就不得不去先理解一波概念了</p>
<h4 id="代理">代理</h4>
<p>理解正向代理与反向代理之前，先大概了解下什么是代理，顾名思义代理就是中间介质的意思，代理服务器是一个中间者，是充当转发请求的角色，原本A和C是可以直连的，现在由于某些原因，比如说网络限制等原因，现在需要有个中间的B来转发他两的请求，这就是代理的意思</p>
<h4 id="正向代理">正向代理</h4>
<p>正向代理就相当于一个跳板机，代理内部的客户端去访问外部的资源，比如国内上不了谷歌，现在有了一台能访问谷歌的机器，就可以用这台机器作为正向代理服务器，请求发到代理服，代理服务器能够访问谷歌，这样由代理去谷歌取到返回数据，再返回给我们，这样我们就能访问谷歌了。</p>
<p>使用正向代理是要在客户端进行设置，比如浏览器设置代理服务器的域名或IP，还有端口等。</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725143149.png" alt=""></p>
<p><strong>正向代理的用途：</strong></p>
<p>（1）访问原来无法访问的资源，如google</p>
<p>​    	（2） 可以做缓存，加速访问资源</p>
<p>（3）对客户端访问授权，上网进行认证</p>
<p>（4）代理可以记录用户访问记录（上网行为管理），对外隐藏用户信息</p>
<h4 id="反向代理-2">反向代理</h4>
<p>反向代理（Reverse Proxy）实际运行方式是指以代理服务器来接受internet上的连接请求，然后将请求转发给内部网络上的服务器，并将从服务器上得到的结果返回给internet上请求连接的客户端，此时代理服务器对外就表现为一个服务器</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725143524.png" alt=""></p>
<p><strong>反向代理的作用：</strong></p>
<p>（1）保证内网的安全，阻止web攻击，大型网站，通常将反向代理作为公网访问地址，Web服务器是内网</p>
<p>（2）负载均衡，通过反向代理服务器来优化网站的负载</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725142917.png" alt=""></p>
<h4 id="总结">总结</h4>
<p><strong>正向代理即是客户端代理, 代理客户端, 服务端不知道实际发起请求的客户端.</strong></p>
<p><strong>反向代理即是服务端代理, 代理服务端, 客户端不知道实际提供服务的服务端</strong></p>
<h4 id="反向代理案例">反向代理案例</h4>
<p>nginx的反向代理是依赖于<a target="_blank" rel="noopener" href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html">ngx_http_proxy_module</a>这个module来实现的，点链接可以看到官网的介绍。</p>
<p>其中可以看到实例：</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725184154.png" alt=""></p>
<p>那么接下来做一下练习，还是用之前静态资源那个案例，静态服务资源我是使用的1880端口去代理的，因为现在练习的是我自己虚拟机，也没有启动其他的服务，所以我就用1880这个端口做练习，实现不修改1880端口代理的情况下，直接使用ip进去就能访问静态资源文件，不加端口号默认是80，所以监听80端口来进行反向代理静态资源服务器。</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>       <span class="number">80</span>;</span><br><span class="line">        <span class="attribute">server_name</span>  localhost;</span><br><span class="line">  </span><br><span class="line">        <span class="attribute">location</span> / &#123;</span><br><span class="line">            <span class="attribute">proxy_pass</span>  http://localhost:1880;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后再reload配置文件，使用ip直接去浏览器访问：</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725185809.png" alt=""></p>
<p>可以看到现在的静态服务器已经可以直接通过ip访问，也就是监听了80端口，反向代理了之前了静态文件服务器，我们现在完全看不到任何关于被反向代理的服务端信息，这也符合反向代理的概念。</p>
<p>这里主要用到了<strong>proxy_pass</strong>这个指令</p>
<p>该指令用于设置被代理服务器的地址。可以是主机名称、IP地址加端口号的形式。</p>
<p>语法结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxy_pass URL;</span><br></pre></td></tr></table></figure>
<p>URL 为被代理服务器的地址，可以包含传输协议、主机名称或IP地址加端口号，URI等。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxy_pass  http:&#x2F;&#x2F;www.123.com&#x2F;uri;</span><br></pre></td></tr></table></figure>
<p>其他的nginx配置的内置变量：<a target="_blank" rel="noopener" href="http://nginx.org/en/docs/varindex.html">nginx内置变量</a>，具体的可以点此链接进去看。</p>
<p>前面已经学习了使用<code>proxy_pass</code>指令来实现反向代理，但是并不完善，网站如何获取主机？</p>
<p>这就要用到<code>proxy_set_header</code>这个指令了</p>
<p><a target="_blank" rel="noopener" href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header">http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header</a></p>
<p>可以看到这么一段</p>
<p><img src="https://gitee.com/zuori/kaiscript/raw/master/images/20200725210032.png" alt=""></p>
<p>具体翻译一下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">句法：	proxy_set_header field value;</span><br><span class="line">默认：	</span><br><span class="line">      proxy_set_header Host       $proxy_host;</span><br><span class="line">			proxy_set_header Connection close;</span><br><span class="line">内容：	http，server，location</span><br><span class="line">允许在传递给代理服务器的请求标头中重新定义或附加字段 。该value可以包含文本，变量，以及它们的组合。当且仅当proxy_set_header 当前级别上未定义任何指令时，这些指令才从上一级继承 。默认情况下，仅重新定义两个字段：</span><br><span class="line"></span><br><span class="line">			proxy_set_header Host       $proxy_host;</span><br><span class="line">			proxy_set_header Connection close;</span><br><span class="line">如果启用了缓存，则标题栏中的字段“ If-Modified-Since”，“ If-Unmodified-Since”，“ If-None-Match”，“ If-Match”，“ Range”和“ If-Range”原始请求不会传递到代理服务器。</span><br><span class="line"></span><br><span class="line">可以像这样传递未更改的“主机”请求标头字段：</span><br><span class="line">			proxy_set_header Host       $http_host;</span><br><span class="line">但是，如果客户端请求标头中不存在此字段，则不会传递任何内容。在这种情况下，最好使用$host变量-它的值等于“主机”请求标头字段中的服务器名称，或者如果不存在此字段，则等于主服务器名称：</span><br><span class="line"></span><br><span class="line">			proxy_set_header  Host  $host;</span><br><span class="line">另外，服务器名称可以与代理服务器的端口一起传递：</span><br><span class="line">			proxy_set_header     Host   $host：$ proxy_port;</span><br><span class="line">如果标头字段的值为空字符串，则此字段将不会传递到代理服务器：</span><br><span class="line">			proxy_set_header Accept-Encoding &quot;&quot;;</span><br></pre></td></tr></table></figure>
<p>这让我想起来前几天在实际项目中遇到的一个问题，客户使用nginx反向代理了minio服务器，但是在反向代理之后，minio服务器进行文件共享也就是文件下载的时候就会报错：</p>
<p>响应代码： 403</p>
<p>报错信息：SignatureDoesNotMatch</p>
<p>报错时配置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen       9000;</span><br><span class="line">    server_name  localhost;</span><br><span class="line"></span><br><span class="line">    #charset koi8-r;</span><br><span class="line">    #access_log  &#x2F;var&#x2F;log&#x2F;nginx&#x2F;host.access.log  main;</span><br><span class="line"></span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">    #        root   &#x2F;usr&#x2F;share&#x2F;nginx&#x2F;html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    proxy_pass http:&#x2F;&#x2F;xx.xx.xx:9000;</span><br><span class="line">    client_max_body_size 200m;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>这里可以看到，在反向代理minio服务的时候，只是简单的配置了下proxy_pass以及client_max_body_size，proxy_pass是用来配置代理的指令，client_max_body_size是用来设置nginx服务器最大上传文件大小，再无其他。</p>
<p>在网上查询了下报错的原因分析：</p>
<p>错误提示SignatureDoesNotMatch签名不正确,后来发现和nginx反向代理在做转发的时候所携带的header有关系.minio在校验signature是否有效的时候,必须从http header里面获取host,而我们这里没有对header作必要的处理.如果源请求未携带这个头,则minio处无法获取请求头中的host,目前我这里测试看请求有携带Host,这里的机制问题出在nginx,nginx没有把这个host转发过去,而用ip的时候Host为 ip:port,这种情况是正常的,这应该和nginx的默认配置proxy_set_header Host $http_host有关系。</p>
<p>然后最后修改了了minio的反向代理配置，修改之后的配置文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">upsream minio&#123;</span><br><span class="line">server xx.xx.xx:9000;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">    listen       9000;</span><br><span class="line">    server_name  eam.shengu.com.cn;  </span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">    proxy_pass http:&#x2F;&#x2F;minio;</span><br><span class="line">    client_max_body_size 1024m;   </span><br><span class="line">    proxy_http_version 1.1;</span><br><span class="line">    proxy_set_header Host $http_host;</span><br><span class="line">    proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">    proxy_set_header X-Forwarded-Proto $scheme;</span><br><span class="line">    proxy_set_header X-Forwarded-Host $http_host;</span><br><span class="line">    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>配置完成后，minio反向代理问题解决，然后下来解读一下配置文件中用到的指令。</p>
<p>首先看看upsream指令的用法</p>
<p>分配方式<br>
Nginx的upstream支持5种分配方式，下面将会详细介绍，其中，前三种为Nginx原生支持的分配方式，后两种为第三方支持的分配方式：<br>
1、轮询</p>
<p>​	轮询是upstream的默认分配方式，即每个请求按照时间顺序轮流分配到不同的后端服务器，如果某个后端服务器down掉后，能自动剔除。<br>
​    upstream backend {<br>
​      server 192.168.1.101:8888;<br>
​      server 192.168.1.102:8888;<br>
​      server 192.168.1.103:8888;<br>
​    }<br>
2、weight<br>
​    轮询的加强版，即可以指定轮询比率，weight和访问几率成正比，主要应用于后端服务器异质的场景下。<br>
​    upstream backend {<br>
​      server 192.168.1.101 weight=1;<br>
​      server 192.168.1.102 weight=2;<br>
​      server 192.168.1.103 weight=3;<br>
​    }<br>
3、ip_hash<br>
​    每个请求按照访问ip（即Nginx的前置服务器或者客户端IP）的hash结果分配，这样每个访客会固定访问一个后端服务器，可以解决session一致问题。<br>
​    upstream backend {<br>
​      ip_hash;<br>
​      server 192.168.1.101:7777;<br>
​      server 192.168.1.102:8888;<br>
​      server 192.168.1.103:9999;<br>
​    }<br>
4、fair<br>
​    fair顾名思义，公平地按照后端服务器的响应时间（rt）来分配请求，响应时间短即rt小的后端服务器优先分配请求。<br>
​    upstream backend {<br>
​      server 192.168.1.101;<br>
​      server 192.168.1.102;<br>
​      server 192.168.1.103;<br>
​      fair;<br>
​    }<br>
5、url_hash<br>
​    与ip_hash类似，但是按照访问url的hash结果来分配请求，使得每个url定向到同一个后端服务器，主要应用于后端服务器为缓存时的场景下。<br>
​    upstream backend {<br>
​      server 192.168.1.101;<br>
​      server 192.168.1.102;<br>
​      server 192.168.1.103;<br>
​      hash $request_uri;<br>
​      hash_method crc32;<br>
​    }<br>
​    其中，hash_method为使用的hash算法，需要注意的是：此时，server语句中不能加weight等参数。<br>
​    关于，如何在负载均衡中使用upstream请参看<a target="_blank" rel="noopener" href="http://blog.chinaunix.net/uid-22312037-id-3954454.html">这里</a>。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">张十三</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://zuori.gitee.io/zaker/2020/08/25/Nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">http://zuori.gitee.io/zaker/2020/08/25/Nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://zuori.gitee.io/zaker" target="_blank">十三不知</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/zaker/tags/Nginx/">Nginx</a><a class="post-meta__tags" href="/zaker/tags/%E5%9F%BA%E7%A1%80%E6%9E%B6%E6%9E%84/">基础架构</a></div><div class="post_share"><div class="addthis_inline_share_toolbox"></div><script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=null" async="async"></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/zaker/2020/09/26/ES%E9%9B%86%E7%BE%A4%E5%AE%89%E8%A3%85/"><img class="prev-cover" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="onerror=null;src='/zaker/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">ES集群安装</div></div></a></div><div class="next-post pull-right"><a href="/zaker/2020/08/04/Nginx%E9%85%8D%E7%BD%AEminio%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86/"><img class="next-cover" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="onerror=null;src='/zaker/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">nginx反向代理minio</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/zaker/2020/08/04/Nginx配置minio反向代理/" title="nginx反向代理minio"><img class="cover" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-04</div><div class="title">nginx反向代理minio</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://gitee.com/zuori/kaiscript/raw/master/images/20210325192506.jpg" onerror="this.onerror=null;this.src='/zaker/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">张十三</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/zaker/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div><div class="card-info-data-item is-center"><a href="/zaker/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div><div class="card-info-data-item is-center"><a href="/zaker/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div><a class="button--animated" id="card-info-btn" href="http://zuori.gitee.io/zaker"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">记录一点东西的地方</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">Nginx学习笔记</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E7%AF%87"><span class="toc-number">1.1.</span> <span class="toc-text">基础篇</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.1.1.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E9%80%89%E6%8B%A9Nginx"><span class="toc-number">1.1.2.</span> <span class="toc-text">为什么选择Nginx</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E4%B8%8E%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.3.</span> <span class="toc-text">安装与使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%89%8D%E7%BD%AE%E5%87%86%E5%A4%87"><span class="toc-number">1.1.3.1.</span> <span class="toc-text">前置准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96Nginx%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85%E5%8C%85"><span class="toc-number">1.1.3.2.</span> <span class="toc-text">获取Nginx源码安装包</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E5%8E%8B%E6%BA%90%E7%A0%81"><span class="toc-number">1.1.3.3.</span> <span class="toc-text">解压源码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E6%BA%90%E7%A0%81"><span class="toc-number">1.1.3.4.</span> <span class="toc-text">编译源码</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#configure%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%A4%A7%E8%87%B4%E8%A7%A3%E8%AF%BB"><span class="toc-number">1.1.3.4.1.</span> <span class="toc-text">configure配置文件大致解读</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85"><span class="toc-number">1.1.3.4.2.</span> <span class="toc-text">编译安装</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8nginx"><span class="toc-number">1.1.3.4.3.</span> <span class="toc-text">启动nginx</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%81%9C%E6%AD%A2nginx"><span class="toc-number">1.1.3.4.4.</span> <span class="toc-text">停止nginx</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%87%8D%E6%96%B0%E5%8A%A0%E8%BD%BD%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">1.1.3.4.5.</span> <span class="toc-text">重新加载配置文件</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%AD%E6%B3%95"><span class="toc-number">1.1.4.</span> <span class="toc-text">基本配置文件语法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BD%8D%E7%BD%AE"><span class="toc-number">1.1.4.1.</span> <span class="toc-text">配置文件位置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%BB%93%E6%9E%84"><span class="toc-number">1.1.4.2.</span> <span class="toc-text">配置文件结构</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8C%87%E4%BB%A4%E7%9A%84%E7%89%B9%E7%82%B9"><span class="toc-number">1.1.4.2.1.</span> <span class="toc-text">指令的特点</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8C%87%E4%BB%A4%E7%9A%84%E5%B1%82%E6%AC%A1"><span class="toc-number">1.1.4.2.2.</span> <span class="toc-text">指令的层次</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%BA%E6%99%AF%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.2.</span> <span class="toc-text">场景实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90WEB%E6%9C%8D%E5%8A%A1"><span class="toc-number">1.2.1.</span> <span class="toc-text">静态资源WEB服务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">1.2.2.</span> <span class="toc-text">反向代理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%90%86"><span class="toc-number">1.2.2.1.</span> <span class="toc-text">代理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">1.2.2.2.</span> <span class="toc-text">正向代理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86-2"><span class="toc-number">1.2.2.3.</span> <span class="toc-text">反向代理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">1.2.2.4.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E6%A1%88%E4%BE%8B"><span class="toc-number">1.2.2.5.</span> <span class="toc-text">反向代理案例</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/zaker/2021/03/30/Kubernetes%E9%9B%86%E7%BE%A4%E7%9B%91%E6%8E%A7%E4%B9%8B%20Metrics%20Server/" title="Kubernetes集群监控之 Metrics Server"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/zaker/img/404.jpg'" alt="Kubernetes集群监控之 Metrics Server"/></a><div class="content"><a class="title" href="/zaker/2021/03/30/Kubernetes%E9%9B%86%E7%BE%A4%E7%9B%91%E6%8E%A7%E4%B9%8B%20Metrics%20Server/" title="Kubernetes集群监控之 Metrics Server">Kubernetes集群监控之 Metrics Server</a><time datetime="2021-03-30T06:46:36.000Z" title="发表于 2021-03-30 14:46:36">2021-03-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zaker/2021/03/26/RabbitMq%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/" title="RabbitMq学习笔记"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/zaker/img/404.jpg'" alt="RabbitMq学习笔记"/></a><div class="content"><a class="title" href="/zaker/2021/03/26/RabbitMq%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/" title="RabbitMq学习笔记">RabbitMq学习笔记</a><time datetime="2021-03-26T01:46:36.000Z" title="发表于 2021-03-26 09:46:36">2021-03-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zaker/2021/03/25/K8s%E5%8F%AF%E8%A7%86%E5%8C%96%E7%9B%91%E6%8E%A7%E4%B9%8B-Weave%20Scope/" title="Kubernetes集群可视化监控之-Weave Scope"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/zaker/img/404.jpg'" alt="Kubernetes集群可视化监控之-Weave Scope"/></a><div class="content"><a class="title" href="/zaker/2021/03/25/K8s%E5%8F%AF%E8%A7%86%E5%8C%96%E7%9B%91%E6%8E%A7%E4%B9%8B-Weave%20Scope/" title="Kubernetes集群可视化监控之-Weave Scope">Kubernetes集群可视化监控之-Weave Scope</a><time datetime="2021-03-25T11:46:36.000Z" title="发表于 2021-03-25 19:46:36">2021-03-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zaker/2021/03/25/Linux%E5%B0%86%E6%99%AE%E9%80%9A%E7%94%A8%E6%88%B7%E5%8A%A0%E5%85%A5sudo%E7%BB%84/" title="Linux将一般用户加入sudo组"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/zaker/img/404.jpg'" alt="Linux将一般用户加入sudo组"/></a><div class="content"><a class="title" href="/zaker/2021/03/25/Linux%E5%B0%86%E6%99%AE%E9%80%9A%E7%94%A8%E6%88%B7%E5%8A%A0%E5%85%A5sudo%E7%BB%84/" title="Linux将一般用户加入sudo组">Linux将一般用户加入sudo组</a><time datetime="2021-03-25T11:46:36.000Z" title="发表于 2021-03-25 19:46:36">2021-03-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zaker/2021/03/03/Kubectl%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" title="kubectl 基本使用"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/zaker/img/404.jpg'" alt="kubectl 基本使用"/></a><div class="content"><a class="title" href="/zaker/2021/03/03/Kubectl%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" title="kubectl 基本使用">kubectl 基本使用</a><time datetime="2021-03-03T11:46:36.000Z" title="发表于 2021-03-03 19:46:36">2021-03-03</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 张十三</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/zaker/js/utils.js"></script><script src="/zaker/js/main.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>